package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.LessorCompanyCO;


@Contract(
        name = "lessorCompany"
)
public final class LessorCompanyContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean LessorCompanyExists(final Context ctx, String lessorCompanyJson){
        ChaincodeStub stub = ctx.getStub();
        LessorCompanyCO lessorCompany = genson.deserialize(lessorCompanyJson, LessorCompanyCO.class);
        String lessorCompanyJSON = stub.getStringState(lessorCompany.formKey());

        return (lessorCompanyJSON != null && !lessorCompanyJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public LessorCompanyCO CreateLessorCompany(final Context ctx, String lessorCompanyJson) {
        ChaincodeStub stub = ctx.getStub();
        if (LessorCompanyExists(ctx, lessorCompanyJson)) {
            String errorMessage = String.format("LessorCompany already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        LessorCompanyCO lessorCompany = genson.deserialize(lessorCompanyJson, LessorCompanyCO.class);
        stub.putStringState(lessorCompany.formKey(), lessorCompanyJson);
        return lessorCompany;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public LessorCompanyCO ReadLessorCompany(final Context ctx, String lessorCompanyId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexLessorCompanyId = LessorCompanyCO.formKey(Long.valueOf(lessorCompanyId));
        String lessorCompanyJSON = stub.getStringState(keyPrefiexLessorCompanyId);
        if (lessorCompanyJSON == null || lessorCompanyJSON.isEmpty()) {
            String errorMessage = String.format("LessorCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        LessorCompanyCO lessorCompany = genson.deserialize(lessorCompanyJSON, LessorCompanyCO.class);
        return lessorCompany;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public LessorCompanyCO UpdateLessorCompany(final Context ctx, String lessorCompanyJson) {
        ChaincodeStub stub = ctx.getStub();
        LessorCompanyCO lessorCompany = genson.deserialize(lessorCompanyJson, LessorCompanyCO.class);

        if (!LessorCompanyExists(ctx, lessorCompanyJson)) {
            String errorMessage = String.format("LessorCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldLessorCompanyJson = stub.getStringState(lessorCompany.formKey());
        LessorCompanyCO oldLessorCompany = genson.deserialize(oldLessorCompanyJson, LessorCompanyCO.class);
        String sortedJson = genson.serialize(lessorCompany);
        stub.putStringState(lessorCompany.formKey(), sortedJson);
        return oldLessorCompany;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteLessorCompany(final Context ctx, String lessorCompanyId) {
        ChaincodeStub stub = ctx.getStub();

        if (!LessorCompanyExists(ctx, lessorCompanyId)) {
            String errorMessage = String.format("LessorCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(lessorCompanyId);
    }
}






